package com.shqcjd.sinopecdemomobile.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.shqcjd.sinopecdemomobile.R;

import java.util.ArrayList;
import java.util.List;

public class DialogMaintainAdapter extends RecyclerView.Adapter<DialogMaintainAdapter.MyViewHolder> {
    private List<String> mList = new ArrayList<>();
    private Context context;
    private setOnClickItemAdapterListener listener;
    private final int count;
    public void setOnClickDialogListener(setOnClickItemAdapterListener listener){
        this.listener = listener;
    }

    public DialogMaintainAdapter(List<String> mList, Context context,int count) {
        this.count= count;
        this.mList = mList;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_dialog_abnormal, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        if(position<mList.size()){
            Glide.with(context).load(mList.get(position)).into(holder.iv_item_dialog_abnormal_img);
            holder.iv_item_dialog_abnormal_camera.setVisibility(View.GONE);
            holder.iv_item_dialog_delete.setVisibility(View.VISIBLE);
            holder.iv_item_dialog_abnormal.setVisibility(View.GONE);
            holder.iv_item_dialog_abnormal_img.setVisibility(View.VISIBLE);
        }else{
            holder.iv_item_dialog_abnormal_img.setVisibility(View.GONE);
            holder.iv_item_dialog_abnormal.setVisibility(View.VISIBLE);
            holder.iv_item_dialog_abnormal_camera.setVisibility(View.VISIBLE);
            holder.iv_item_dialog_delete.setVisibility(View.GONE);
        }
        holder.iv_item_dialog_abnormal_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    listener.onClickAddPic(position);
                }
            }
        });
        holder.iv_item_dialog_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    listener.onClickDeletePic(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size()== count ? count : mList.size()+1;
    }

    public interface setOnClickItemAdapterListener{
        void onClickAddPic(int pos);
        void onClickDeletePic(int pos);
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_item_dialog_abnormal, iv_item_dialog_abnormal_img,
                iv_item_dialog_delete, iv_item_dialog_abnormal_camera;
        public MyViewHolder(View itemView) {
            super(itemView);
            iv_item_dialog_abnormal_img = itemView.findViewById(R.id.iv_item_dialog_abnormal_img);
            iv_item_dialog_delete = itemView.findViewById(R.id.iv_item_dialog_delete);
            iv_item_dialog_abnormal = itemView.findViewById(R.id.iv_item_dialog_abnormal);
            iv_item_dialog_abnormal_camera = itemView.findViewById(R.id.iv_item_dialog_abnormal_camera);
        }
    }
}

